package com.hcxy.filetransform.util;

import java.io.File;
import java.text.DecimalFormat;

import android.annotation.SuppressLint;

import com.hcxy.filetransform.domain.FileTranslateInfo;

public class FileUtils {
	@SuppressLint("NewApi")
	public static FileTranslateInfo getFileInfo(String path){
		File file = new File(path);
		FileTranslateInfo finfo = null;
		if (file.exists()) {
			int index = path.lastIndexOf(File.separator);
			String name  = path.substring(index+1,path.length());
			String expandName = name.substring(name.lastIndexOf(".")+1,name.length());
			finfo =  new FileTranslateInfo();
			long size = file.length();
			finfo.setFilelength(size);
			finfo.setFileName(name);
			finfo.setExpandName(expandName);
			finfo.setToken(System.currentTimeMillis()+"");
		}
		return finfo;
	}
	
	@SuppressLint("NewApi")
	public static FileTranslateInfo getFileInfo(File file){
		String path = file.getAbsolutePath();
		FileTranslateInfo finfo = null;
		if (file.exists()) {
			int index = path.lastIndexOf("/");
			String name  = path.substring(index,path.length()+1);
			String expandName = name.substring(name.lastIndexOf(".")+1,name.length());
			finfo =  new FileTranslateInfo();
			long size = file.getTotalSpace();
			finfo.setFilelength(size);
			finfo.setFileName(name);
			finfo.setExpandName(expandName);
		}
		return finfo;
	}
	
	public static String sizeToStr(long size){
		DecimalFormat df = new DecimalFormat("0.00");
		if(size<1024){	//	< 1kb
			return size+"byte";
		}else if (size>1024 && size<1048576) {	//>  1kb <1mb
			double s = (double)(size/1024);
			return (df.format(s))+"kb";
		}else{	// > 1mb	<1g
			double s = (double)(size/1024/1024);
			return (df.format(s))+"mb";
		}
	}
	
}
